package com.kumi.shop.coupon.service.impl;

import com.kumi.common.to.MemberPriceTo;
import com.kumi.common.to.SkuReductionTo;
import com.kumi.shop.coupon.bean.MemberPrice;
import com.kumi.shop.coupon.bean.SkuLadder;
import com.kumi.shop.coupon.service.MemberPriceService;
import com.kumi.shop.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kumi.common.utils.PageUtils;
import com.kumi.common.utils.Query;

import com.kumi.shop.coupon.mapper.SkuFullReductionMapper;
import com.kumi.shop.coupon.bean.SkuFullReduction;
import com.kumi.shop.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReduction> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReduction> page = this.page(
                new Query<SkuFullReduction>().getPage(params),
                new QueryWrapper<SkuFullReduction>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        SkuLadder skuLadder = new SkuLadder();
        skuLadder.setSkuId(skuReductionTo.getSkuId());
        skuLadder.setFullCount(skuReductionTo.getFullCount());
        skuLadder.setDiscount(skuReductionTo.getDiscount());
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadder);
        }


        SkuFullReduction skuFullReduction = new SkuFullReduction();
        BeanUtils.copyProperties(skuReductionTo,skuFullReduction);
        if (skuFullReduction.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            this.save(skuFullReduction);
        }

        List<MemberPriceTo> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPrice> collect = memberPrice.stream().map(item -> {
            MemberPrice price = new MemberPrice();
            price.setSkuId(skuReductionTo.getSkuId());
            price.setMemberLevelId(item.getId());
            price.setMemberLevelName(item.getName());
            price.setMemberPrice(item.getPrice());
            price.setAddOther(1);
            return price;
        }).filter(item -> {
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}